package code.tools.commponents
{
	import code.main.App;
	import code.structure.manager.language.LanguageManager;
	import code.untils.ItemTypeConst;
	import code.structure.manager.bitmap.YDisplayConvert;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	
	/**
	 *帮助类
	 * @author liangqian
	 *
	 */
	public class Helpers
	{
		public function Helpers()
		{
		}
		private static var txtFormat:TextFormat = new TextFormat();
		
		/**
		 * 快速格式化一个 text field 实例,默认高度20
		 * @param parent 父级容器
		 * @param x
		 * @param y
		 * @param textFieldAutoSize 自动对齐方式 TextFieldAutoSize
		 * @param textColor 要格式化的文字的颜色
		 * @param useEmbedFont 嵌入字体
		 * @param textFormatAlignment 自动对齐方式 TextFormatAlign
		 * @return 格式化后的tf
		 */
		public static function FormatTextField(parent:DisplayObjectContainer, x:Number = 0, y:Number = 0, width:int = 100, textFieldAutoSize:String = 'left', textColor:uint = 0x512F2F, useEmbedFont:String = '', size:int = 12, textFormatAlignment:String = 'left'):TextField
		{
			var tf:AutoTextField = new AutoTextField();
			tf.x = x;
			tf.y = y;
			tf.width = width;
			tf.autoSize = textFieldAutoSize;
			//			tf.height = 20;
			tf.textColor = textColor;
			txtFormat.size = size;
			txtFormat.align = textFormatAlignment;
			if (useEmbedFont == '')
				tf.embedFonts = false;
			else
			{
				tf.embedFonts = true;
				txtFormat.font = useEmbedFont;
			}
			tf.defaultTextFormat = txtFormat;
			
			//			if (addShadow)
			//				tf.filters=TextBitmapData.TEXT_SHADOW_FILTER;
			
			if(parent != null)
				parent.addChild(tf);
			return tf;
		}
		
		
		/**
		 *替换创建一个新的文本框 
		 * @param tf
		 * @param fontName
		 * 
		 */
		public static function replaceTf(tf:TextField, fontName:String,isEmbedFonts:Boolean=true,isZoom:Boolean = false):TextField
		{
			var newtf:AutoTextField = new AutoTextField();
			var fmt:TextFormat = tf.defaultTextFormat;
			var rot:Number = tf.rotation;
			var tfSource:String = tf.text;
			newtf.visible = tf.visible;
			tf.rotation = 0;
			if(isZoom)newtf.openZoom(tf.width,tf.height);
			
			fmt.kerning = true;
			fmt.font = fontName;
			
			//获取所有设置
			newtf.text = tfSource;
			newtf.alpha = tf.alpha;
			newtf.antiAliasType = AntiAliasType.ADVANCED;
			//			newtf.defaultTextFormat = fmt;
			newtf.autoSize = tf.autoSize;
			newtf.embedFonts = isEmbedFonts;
			newtf.gridFitType = tf.gridFitType;
			newtf.height = tf.height;
			newtf.mouseEnabled = false;//tf.mouseEnabled;
			newtf.mouseWheelEnabled = false;
			newtf.multiline = tf.multiline;
			newtf.name = tf.name;
			newtf.selectable = tf.selectable;
			newtf.sharpness = tf.sharpness;
			newtf.textColor = tf.textColor;
			newtf.thickness = tf.thickness;
			newtf.width = tf.width;
			newtf.wordWrap = tf.wordWrap;
			newtf.x = tf.x;
			newtf.y = tf.y;
			newtf.cacheAsBitmap = tf.cacheAsBitmap;
			newtf.filters = tf.filters;
			newtf.type = tf.type;
			newtf.scaleX = tf.scaleX;
			newtf.scaleY = tf.scaleY;
			//			newtf.setTextFormat(fmt);
			newtf.defaultTextFormat = fmt;
			newtf.text = tfSource;
			newtf.applyDefaultAutoSize();
			//			newtf.htmlText = tf.htmlText;
			
			
			newtf.rotation = rot;
			tf.rotation = rot;
			if (tf.parent)
			{
				var pos:int = tf.parent.getChildIndex(tf);
				var p:DisplayObjectContainer = tf.parent;
				p.removeChild(tf);
				p.addChildAt(newtf, pos);
				//				try 
				//				{
				//					p[newtf.name] = newtf;
				//				}
				//				catch(e:Error)
				//				{
				//					trace("replace font error!")
				//				}
				tf = p.getChildAt(pos) as TextField;//p[newtf.name];
				return tf;
			}
			return null;
		}
		
		/**
		 * 处理容器内文本框 
		 * @param parent
		 * @param name
		 * @param text
		 * @param font
		 * @return 
		 * 
		 */		
		public static function processTf(parent:Sprite,name:String,text:String,font:String,isEmbedFonts:Boolean=true,isZoom:Boolean = false):TextField
		{
			if(text == null) text = '';
			var tf:TextField = parent.getChildByName(name) as TextField;
			tf = Helpers.replaceTf(tf,font,isEmbedFonts,isZoom);
			tf.htmlText = text;
			tf.mouseEnabled = false; 
			return tf;
		}
		
		/**
		 * 处理通过把文本框作为遮罩实现的艺术字 
		 * @param con
		 * 
		 */		
		public static function processWordart(con:Sprite,text:String,font:String,isEmbedFonts:Boolean = true,isZoom:Boolean = true):TextField
		{
			con.mouseChildren = false;
			con.mouseEnabled = false;
			var tf:TextField;
			for(var i:int = 0;i < con.numChildren;i++)
			{
				if(con.getChildAt(i) is TextField)
					tf = con.getChildAt(i) as TextField;
			}
			if(tf == null)return null;
			if(text == null)text = '';
			tf = Helpers.replaceTf(tf,font,isEmbedFonts,isZoom);
			tf.text = text;
			con.mask = tf;
			return tf;
		}
		
		/**
		 * 
		 * @param bm
		 * @param obj
		 * @return 
		 * 
		 */		
		public static function drawConvertBm(bm:Bitmap,obj:DisplayObject):Bitmap
		{
			var array:Array = YDisplayConvert.drawBitMapData(obj);
			if(bm == null)
				bm = new Bitmap(null,"auto",true);
			bm.bitmapData = array[0].clone();
			return bm;
		}
		
		
		/**
		 *星星动画停止 
		 * @param starMc
		 * 
		 */		
		public static function clearDessertStarLevelAni(starMc:MovieClip):void
		{
			var i:int;
			for(; i < starMc.numChildren; i++)
			{
				((starMc.getChildAt(i) as MovieClip).getChildByName('starAni') as MovieClip).gotoAndStop(13);
			}
		}
		
		
		
		/**
		 * 返回字符的字节长度
		 * @return
		 */
		public static function checkStringByteLength(str:String):int
		{
			var length:int;
			var len:int = str.length;
			for (var i:int; i < len; i++)
				length += str.charCodeAt(i) > 255 ? 2 : 1;
			return length;
		}
		
		/**
		 * 返回 分钟+秒 时间文本（例如 30：08）
		 * @param t 秒
		 * @return
		 *
		 */
		public static function getTimeText(t:int):String
		{
			if (t < 0)
				t = 0;
			return zeroize((t / 60) % 60) + ":" + zeroize(t % 60);
		}
		
		/**
		 * 返回小时 + 分钟 + 秒的时间文本（例如 15：30：08）
		 * @param t 秒
		 * @return
		 *
		 */
		public static function getHourTimeText(t:int):String
		{
			if (t < 0)
				t = 0;
			return zeroize(t / 3600) + ":" + zeroize((t / 60) % 60) + ":" + zeroize(t % 60);
		}
		
		/**
		 * 如果时间大于一天 返回 天 + 'days' ,如果时间小于一天  返回小时 + 分钟 + 秒的时间文本（例如 15：30：08）
		 * @param t
		 * @return 
		 * 
		 */		
		public static function getNormTimeText(t:int):String
		{
			if(t < 0)
				t = 0;
			var day:int = Math.floor(t/86400);
			if(day > 0)
				return day + ' ' +LanguageManager.getLanguageInfo('DESCRIPTION_DAYS');
			else
				return zeroize(t / 3600) + ":" + zeroize((t / 60) % 60) + ":" + zeroize(t % 60);
		}
		
		/**
		 * 返回小时 + 分钟 的时间文本（例如 15：30）
		 * @param t 秒
		 * @return
		 *
		 */
		public static function getMinuteTimeText(t:int):String
		{
			if (t < 0)
				t = 0;
			return zeroize(t / 3600) + ":" + zeroize((t / 60) % 60);
		}
		
		/**
		 *补零，一般用于倒计时00：00：00，需要在前方补零的情况
		 * @param num
		 * @return
		 *
		 */
		public static function zeroize(num:int):String
		{
			return num > 9 ? '' + num : "0" + num;
		}
		
		/**
		 *返回X小时/X分钟/X秒的文本
		 * @param t 秒数
		 * @return
		 *
		 */
		public static function getTimeTextWithUnit(t:int):String
		{
			var n:int;
			var s:int;
			var h:int;
			if (t <= 60)
				return t + '  ' + LanguageManager.getLanguageInfo('SECOND');
			else
			{
				h = t/3600;//小时
				var str:String = '';
				if(h >= 1)
					str += h + LanguageManager.getLanguageInfo('HOUR');
				n = (t / 60) % 60;//分钟
				if(n != 0)
					str += n + LanguageManager.getLanguageInfo('MINUTE');
				s = t % 60;//秒
				if (s != 0)
					str += s + LanguageManager.getLanguageInfo('SECOND');
				return str;
			}
		}
		
		/**
		 *返回带Html样式的X小时/X分钟/X秒的文本
		 * @param t 秒数
		 * @return
		 *
		 */
		public static function getTimeHtmlTextWithUnit(t:int):String
		{
			var n:int;
			var s:int;
			var h:int;
			if (t <= 60)
				return 	"<Font size = '16'>" + t + "</Font>"+"<Font size = '18'>" + LanguageManager.getLanguageInfo('SECOND') + "</Font>";
			else
			{
				h = t/3600;//小时
				var str:String = '';
				if(h >= 1)
					str += "<Font size = '16'>" + h + "</Font>"+"<Font size = '18'>" + LanguageManager.getLanguageInfo('HOUR') + " </Font>";
				n = (t / 60) % 60;//分钟
				if(n != 0)
					str += "<Font size = '16'>" + n + "</Font>"+"<Font size = '18'>" + LanguageManager.getLanguageInfo('MINUTE') + " </Font>";
				s = t % 60;//秒
				if (s != 0)
					str += "<Font size = '16'>" + s + "</Font>"+"<Font size = '18'>" + LanguageManager.getLanguageInfo('SECOND') + "</Font>";
				return str;
			}
		}
		
		/**
		 *根据秒数，返回天数 
		 * @param t 秒数
		 * @return 
		 * 
		 */		
		public static function getDaysWithUnit(t:int):int
		{
			var s:int = 24* 60 * 60;
			if(t < s)return 0;
			return Math.floor(t / s);
		}
		
		public static function drawBmd(c:*, shape:Sprite):Sprite
		{
			if (c is BitmapData)
			{
				shape = new Sprite();
				const b:BitmapData = (c as BitmapData).clone();
				var bb:Bitmap = new Bitmap( b,"auto",true);
				/*shape.graphics.beginBitmapFill(b, null, true, true);
				shape.graphics.drawRect(0, 0, b.width, b.height);
				shape.graphics.endFill();*/
				shape.addChild(bb);
			}
			else
			{
				var dis:* = new c();
				if (dis as BitmapData)
				{
					shape = new Sprite();
					const d:BitmapData = (dis as BitmapData).clone();
					shape.graphics.beginBitmapFill(d,null,true,true);
					shape.graphics.drawRect(0, 0, d.width, d.height);
					shape.graphics.endFill();
				}
				else
				{
					shape = dis as Sprite;
				}
			}
			if(shape && shape as MovieClip)
			{
				(shape as MovieClip).gotoAndStop(1);
			}
			return shape;
		}
		
		public static function drawMc(c:*, shape:MovieClip):MovieClip
		{
			if (c is BitmapData)
			{
				shape = new MovieClip();
				const b:BitmapData = (c as BitmapData).clone();
				shape.graphics.beginBitmapFill(b, null, true, true);
				shape.graphics.drawRect(0, 0, b.width, b.height);
				shape.graphics.endFill();
			}
			else
			{
				var dis:* = new c();
				if (dis as BitmapData)
				{
					shape = new MovieClip();
					const d:BitmapData = (dis as BitmapData).clone();
					shape.graphics.beginBitmapFill(d);
					shape.graphics.drawRect(0, 0, d.width, d.height);
					shape.graphics.endFill();
				}
				else
				{
					shape = dis as MovieClip;
				}
			}
			return shape;
		}
		
		
		public static function getBmd(c:*):BitmapData
		{
			var b:BitmapData;
			if (c is BitmapData)
				b = (c as BitmapData).clone();
			else
			{
				var dis:* = new c();
				if (dis as BitmapData)
					b = (dis as BitmapData).clone();
			}
			return b;
		}
		
		
		/**
		 *设置带有遮罩的文字 
		 * @param tf 文本框
		 * @param t 文字内容
		 * @param bm 
		 * @param shade 遮罩
		 * @param filters 滤镜
		 * @param parent bm将被添加到哪里
		 * @return bm
		 * 
		 */		
		public static function setTextWithShadow(tf:TextField,t:String,bm:Bitmap,shade:Sprite,filters:Array,parent:Sprite):Bitmap
		{
			tf.text = t;
			bm = Helpers.drawConvertBm(bm,tf);
			bm.cacheAsBitmap = true;
			shade.filters = filters;
			parent.addChild(bm);
			shade.mask = bm;
			bm.x = shade.x;
			bm.y = shade.y;
			return bm;
		}
		
//		/**
//		 *返回数据 SpendData，为扣钱动画做准备
//		 * @param price 物品价格
//		 * @param buyNum 购买数量
//		 * @param PriceType 货币类型
//		 * @param aniXpos 动画出现时的x坐标，若为-1，则舞台水平居中
//		 * @param aniYpos 动画出现时的y坐标，若为-1，则舞台垂直居中
//		 * @return 
//		 * 
//		 */		
//		public static function getSpendData(price:int,buyNum:int,PriceType:int,aniXpos:int = -1,aniYpos:int = -1):SpendData
//		{
//			var pay:SpendData = new SpendData(-(price * buyNum), PriceType);
//			pay.x = aniXpos == -1 ? App.appstage.stageWidth * .5 : aniXpos + 20;
//			pay.y = aniYpos == -1 ? App.appstage.stageHeight * .5 : aniYpos;
//			return pay;
//		}
	}
}